<template>
  <el-dialog
    title="主干规则组"
    :visible.sync="showVisbale"
    width="80%">
    <el-form :inline="true" :model="dataForm" >
      <el-row>
        <el-col :span="8">
          <el-form-item label="规则组标识">
            <el-input v-model="dataForm.groupIdentify" placeholder="规则组标识" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="规则组名称">
            <el-select v-model="dataForm.groupName" filterable clearable placeholder="请选择">
              <el-option
                v-for="item in allGroupInfo"
                :key="item.groupName"
                :label="item.groupName"
                :value="item.groupName">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="getDataList()"></el-button>
            <el-button icon="el-icon-refresh" @click="resetData()"></el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <div class="contain">
      <!-- 操作按钮 -->
      <el-row>
        <el-col :span="24" class="pb15 pt5">
          <el-button v-if="branchType==='2'" type="info" plain size="mini" style="margin-left: 10px;" @click="copyGroups()">复制规则组</el-button>
        </el-col>
      </el-row>
      <!-- 查询结果列表 -->
      <div class="table_container">
        <!-- 列表 -->
        <el-table
          :data="dataList"
          ref="multipleTable"
          border
          style="width: 100%; ">
          <el-table-column type="selection"></el-table-column>
          <el-table-column
            prop="groupIdentify"
            header-align="center"
            align="center"
            label="规则组标识">
          </el-table-column>
          <el-table-column
            prop="groupName"
            header-align="center"
            align="center"
            label="规则组名称">
          </el-table-column>
          <el-table-column
            prop="groupDesc"
            header-align="center"
            align="center"
            label="规则组描述">
          </el-table-column>

        </el-table>
        <!-- 分页 -->
        <el-row>
          <el-col :span="24">
            <div class="pagination">
              <!-- 分页 -->
              <el-pagination
                @size-change="sizeChangeHandle"
                @current-change="currentChangeHandle"
                :current-page="page.pageIndex"
                :page-sizes="[10, 20, 50, 100]"
                :page-size="page.pageSize"
                :total="page.totalPage"
                layout="total, sizes, prev, pager, next, jumper">
              </el-pagination>
            </div>
          </el-col>
        </el-row>
      </div>
    </div>

  </el-dialog>
</template>

<script>
    import {pageGroupData, createBranch} from '@/api/ruleConfig/branchGroupInfo'
    import {listGroupData} from '@/api/ruleConfig/groupInfo'
    import constant from "@/api/constant";

    export default {
        data() {
            return {
                branchCode:'',
                branchName:'',
                branchType:'',
                allGroupInfo:[],
                resultMsg:'',
                resultCode:'',
                demoData:[],
                showDemoVisbale:false,
                loading: false,
                templateStr: '',
                showVisbale: false,
                branchForm: {
                    groupName: '',
                    groupIdentify: '',
                    branchName: '',
                    id: '',
                },
                dataForm: {
                    groupName: '',
                    groupIdentify: ''
                },
                dataList: [],
                dataListLoading: false,
                addOrUpdateVisible: false,
                testConditionVisible: false,
                page: {
                    pageIndex: 1,
                    pageSize: 10,
                    totalPage: 0
                }
            }
        },
        components: {
        },
        activated() {

        },
        methods: {
            init(branchCode,branchName,branchType){
                this.branchName = branchName
                this.branchCode = branchCode
                this.branchType = branchType
                this.showVisbale = true
                this.getDataList()
                this.initGroupInfo()
            },
            initGroupInfo(){
                listGroupData({}).then(res=>{
                    if(res.data.data){
                        this.allGroupInfo = res.data.data
                    }
                })
            },
            // 重置查询条件
            resetData() {
                this.dataForm.groupName = ''
                this.dataForm.groupIdentify = ''
            },


            copyGroups() {
                this.showVisbale = true
                this.$store.dispatch('loading/CHANGE_LOADING', true)
                let arr = []
                this.$refs.multipleTable.selection.forEach(item => {
                    arr.push({id: item.id})
                })
                if (arr.length <= 0) {
                    this.$message.error("请选择规则组！")
                    this.$store.dispatch('loading/CHANGE_LOADING', false);
                    return
                }
                let params = {
                    branchName: this.branchName,
                    branchCode: this.branchCode,
                    ids: JSON.stringify(arr)
                }
                createBranch(params).then(res=>{
                    if(res.data.resultCode===0){
                        this.showVisbale = false
                        this.$emit('refreshDataList')
                    }else{
                        this.$message.error(res.data.resultMsg)
                    }
                    this.$store.dispatch('loading/CHANGE_LOADING', false);
                }).catch(err=>{
                    this.$store.dispatch('loading/CHANGE_LOADING', false);
                })
            },
            // 每页数
            sizeChangeHandle(val) {
                this.page.pageSize = val
                this.page.pageIndex = 1
                this.getDataList()
            },
            // 当前页
            currentChangeHandle(val) {
                this.page.pageIndex = val
                this.getDataList()
            },
            // 获取数据列表
            getDataList() {
                this.$store.dispatch('loading/CHANGE_LOADING', true)
                let params = {
                    pageNumber: this.page.pageIndex,
                    pageSize: this.page.pageSize,
                    groupName: this.dataForm.groupName,
                    groupIdentify: this.dataForm.groupIdentify
                }
                pageGroupData(params).then(res => {
                    this.$store.dispatch('loading/CHANGE_LOADING', false)
                    this.dataList = res.data.data.list
                    this.page.totalPage = res.data.data.totalCount
                }).catch(err=>{
                    this.$store.dispatch('loading/CHANGE_LOADING', false)
                })
            },

        },
        filters: {
            branchOperTypeFilter: function (value) {
                return constant.getLabel(value,constant.ruleConfig.branchOperType)
            }
        }
    }
</script>
<style>
  body .el-table th.gutter {
    display: table-cell !important
  }
  .txt{
    color:red;
  }
</style>
